package com.yxh.bigdata.mapreduce.join;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class JoinReducer extends Reducer<Text,JoinOrderBean, JoinOrderBean, NullWritable> {

    @Override
    protected void reduce(Text key, Iterable<JoinOrderBean> values, Context context) throws IOException, InterruptedException {

        JoinOrderBean bean = new JoinOrderBean();
        Iterator<JoinOrderBean> iterator = values.iterator();
        while (iterator.hasNext()){
            JoinOrderBean orderBean = iterator.next();
            if(StringUtils.isNotBlank(orderBean.getOid())){
                bean.setOid(orderBean.getOid());
            }
            if(StringUtils.isNotBlank(orderBean.getOdate())){
                bean.setOdate(orderBean.getOdate());
            }
            if(StringUtils.isNotBlank(orderBean.getPid())){
                bean.setPid(orderBean.getPid());
            }
            if(StringUtils.isNotBlank(orderBean.getAmount())){
                bean.setAmount(orderBean.getAmount());
            }
            if(StringUtils.isNotBlank(orderBean.getPname())){
                bean.setPname(orderBean.getPname());
            }
            if(StringUtils.isNotBlank(orderBean.getPrice())){
                bean.setPrice(orderBean.getPrice());
            }
        }
        context.write(bean, NullWritable.get());
    }
}
